/*******************************************************************************
 * Copyright (c) 2008 Bartosz Michalik and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bartosz Michalik (bartosz.michalik@gmail.com)
 *******************************************************************************/

package org.eclipse.pde.internal.core.product;

import java.io.PrintWriter;
import org.eclipse.pde.internal.core.iproduct.IProductCustomizationConstants;
import org.eclipse.pde.internal.core.iproduct.IProductModel;
import org.w3c.dom.Node;

public class ChangeNameTransform extends ProductTransform {

	public static final String TRANSFORM_ID = "rename"; //$NON-NLS-1$
	private static final String NAME_ATT = "name"; //$NON-NLS-1$

	private String fName;

	public ChangeNameTransform(IProductModel model) {
		super(model);
	}

	public String getName() {
		return fName;
	}

	public void setName(String name) {
		fName = name;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 8149980752425208286L;

	public void write(String indent, PrintWriter writer) {
		if (fXPath != null) {
			StringBuffer sb = new StringBuffer();
			sb.append(indent).append("<"); //$NON-NLS-1$
			sb.append(TRANSFORM_ID).append(" ").append(TARGET_ATT); //$NON-NLS-1$
			sb.append("=\"").append(fXPath).append("\" ");//$NON-NLS-1$ //$NON-NLS-2$
			sb.append(NAME_ATT).append("=\"").append(fName).append("\" "); //$NON-NLS-1$ //$NON-NLS-2$
			sb.append("/>"); //$NON-NLS-1$
			writer.println(sb.toString());
		} // else TODO add error login

	}

	public void writeTransformEntry(String indent, PrintWriter writer, String type) {
		int idx = fXPath.indexOf('/', 1);
		if (fXPath != null && idx != -1) {
			int idStart = fXPath.lastIndexOf('/');
			String id = fXPath.substring(idStart + 1, fXPath.indexOf("[", idStart));
			if (IProductCustomizationConstants.TYPE_XSLT.equals(type)) {

				writer.print(indent);
				writer.print("<xsl:template match=\""); //$NON-NLS-1$
				writer.print("/plugin"); //$NON-NLS-1$
				writer.print(fXPath.substring(idx));
				writer.println("\" >"); //$NON-NLS-1$
				writer.println(indent + indent + "<" + id + ">");
				writer.println(indent + indent + indent + "<xsl:attribute name=\"name\">" + fName + "</xsl:attribute>");
				writer.println(indent + indent + indent + "<xsl:copy-of select=\"node()|@*[not(name()='name')]\" />");
				writer.println(indent + indent + "</" + id + ">");
				writer.print(indent);
				writer.println("</xsl:template>"); //$NON-NLS-1$
			}
		} // else TODO add error login
	}

	protected void parseData(Node node) {
		Node nameItem = node.getAttributes().getNamedItem(NAME_ATT);
		if (nameItem == null || nameItem.getNodeValue() == null)
			throw new IllegalStateException(NAME_ATT + " attribute is missing"); //$NON-NLS-1$
		fName = nameItem.getNodeValue();
	}

}
